package cn.com.aiton.common.netty.codec;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NettyPosindaEncoder extends MessageToByteEncoder<NettyPosindaProtocol> {
    static Logger LOG = LoggerFactory.getLogger(NettyPosindaEncoder.class);
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, NettyPosindaProtocol customProtocol, ByteBuf byteBuf) throws Exception {
        byteBuf.writeByte(customProtocol.getProtocol());
        byteBuf.writeByte(customProtocol.getVersion());
        byteBuf.writeShort(customProtocol.getRetain());
        LOG.warn("customProtocol.getBody().length()："+customProtocol.getBody().getBytes().length+"    customProtocol.getLen():"+customProtocol.getLen());
        byteBuf.writeInt(customProtocol.getLen());
        byteBuf.writeBytes(Unpooled.copiedBuffer(customProtocol.getBody(), CharsetUtil.UTF_8));
    }
}
